package com.arcbbs.controller;

import com.arcbbs.entity.*;
import com.arcbbs.exception.BaseException;
import com.arcbbs.mapper.FormArticleMapper;
import com.arcbbs.mapper.ForumCommentMapper;
import com.arcbbs.mapper.UserMessageMapper;
import com.arcbbs.result.PageResult;
import com.arcbbs.result.Result;
import com.arcbbs.service.FormArticleService;
import com.arcbbs.service.ForumCommentService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private FormArticleService formArticleService;
    @Resource
    private FormArticleMapper formArticleMapper;
    @Resource
    private UserMessageMapper userMessageMapper;
    @Resource
    private ForumCommentService forumCommentService;
    @Resource
    private ForumCommentMapper forumCommentMapper;
    @RequestMapping("/list")
    public Result list(HttpSession httpSession,String title, Integer pBoardId, Integer boardId, Integer pageNo){
        ForumArticleQuery forumArticleQuery = new ForumArticleQuery();
        forumArticleQuery.setBoardId(boardId == null || boardId == 0 ? null : boardId);
        forumArticleQuery.setpBoardId(pBoardId);
        forumArticleQuery.setPageNo(pageNo);
        forumArticleQuery.setTitle(title);


        PageResult list = formArticleService.selectbypage(forumArticleQuery, 0);


        return Result.success(list);
    }
    @RequestMapping("/comment")
    public Result comment(HttpSession httpSession,Integer pageNo){
        PageResult list=forumCommentService.selectbypage(pageNo);
        return Result.success(list);
    }
    @RequestMapping("/commentdelete")
    public Result commentdelete(String commentId){
        ForumComment forumComment=forumCommentService.getForumCommentById(Integer.valueOf(commentId));
        if (forumComment == null){
            throw new BaseException("该评论不存在");

        }
        forumCommentMapper.deletebyid(commentId);
        return Result.success();

    }
    @RequestMapping("/delete")
    public Result delete(HttpSession httpSession,String articleId){
        ForumArticleVo forumArticle=formArticleService.getbyid(articleId);
        if(forumArticle == null){
            throw new BaseException("该文章不存在");

        }
        formArticleMapper.deletebyid(articleId);
        UserMessage userMessage=new UserMessage();
        userMessage.setArticleTitle(forumArticle.getTitle());
       userMessage.setArticleId(forumArticle.getArticleId());
       userMessage.setReceivedUserId(forumArticle.getUserId());
       userMessage.setMessageType(0);
userMessage.setCreateTime(new Date());
userMessage.setMessageContent("系统删除了你的文章");
        userMessageMapper.insert(userMessage);
        return Result.success();

    }
}
